package com.css.fxfzypg.modules.saresults.task;

import cn.hutool.core.date.DateUtil;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.fxfzypg.constants.FxfzConstants;
import com.css.fxfzypg.modules.saresults.entity.DzqhykzsfglzxtDrillEntity;
import com.css.fxfzypg.modules.saresults.repository.DrillHoleQueryRepository;
import com.css.fxfzypg.util.PlatformDateUtils;
import com.css.fxfzypg.util.PlatformObjectUtils;
import com.css.fxfzypg.util.RestTemplateUtil;
import lombok.extern.slf4j.Slf4j;
import org.slw.common.helper.PropsHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.*;

@Component
@Slf4j
public class JcsjTask {

    @Resource
    private RestTemplateUtil restTemplateUtil;

    @Autowired
    private DrillHoleQueryRepository drillHoleQueryRepository;


    Properties configGroup = PropsHelper.loadProps("slw-config.properties");
    String url = configGroup.getProperty("restTemplateUrl") + "/datacenter/jcRfcpDrill/dzqhykzsfglzxt/list";


    /**
     * 调基础数据系统接口，存钻孔数据
     * @return
     */
    //定时任务(0 0/5 * * * ?)  每五分钟执行一次
//    @Scheduled(cron = "0 0/5 * * * ?")
    public void saveHlDataByJcsj() {
        try {
            Map<String, Object> paramMap = new HashMap<>();
            String id = drillHoleQueryRepository.queryMaxCode();
            if(StringUtils.isEmpty(id)){
                paramMap.put("index","0");
            }else {
                paramMap.put("index",id);
            }
            ResponseEntity<String> result = restTemplateUtil.sendHttpRequestPj(url,paramMap);
            if(result.getBody()!=null){
                JSONObject json = JSONObject.parseObject(result.getBody());
                String data = json.getString("data");
                List<DzqhykzsfglzxtDrillEntity> dataList = JSON.parseArray(data, DzqhykzsfglzxtDrillEntity.class);
                if(PlatformObjectUtils.isNotEmpty(dataList)){
                    dataList.forEach(it->{
                        it.setDataType(FxfzConstants.DZQH_JCSJ_MODEL);
                        it.setIsDelete("0");
                        drillHoleQueryRepository.saveHlByJcsj(it);
                    });
                }
            }
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}
